//
//  STMiddleView.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/17.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

/// 每一张图片的宽度（高度）
private let imageButton_W_H = (kScreen_W-2*cellMargin-(CGFloat(cols)-1)*imageMargin)/CGFloat(cols)

/// 每一行排列多少张，即列数
private let cols = 3
/// 每一张图片之间的间距
private let imageMargin:CGFloat = 4.0

class STPictureView: UIView {
   
    /// 微博配图数组
    var pictureArray:[NSURL]?{
        didSet{
//            STPrint(items: "微博配图来数据了==>\(pictureArray)")
            let size=layoutPicture(pictureArray)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
        }
    }

    /// 布局微博配图
    private func layoutPicture(pictureURLs: [NSURL]?)->CGSize{
        let count = pictureURLs?.count ?? 0

        /// 3列
        var numberOfColumn = 3
        if count == 1 {
            numberOfColumn = 1
        } else if count == 2 || count == 4 {
            numberOfColumn = 2
        }
        var index = 0
        for button in imageButtonArray {
            if index >= count {
                // 隐藏
                button.hidden = true
            } else {
                // 显示, 布局
                button.hidden = false
                // 计算每个imageView在哪列
                let column = index % numberOfColumn
                // 计算每个imageView在哪行
                let row = index / numberOfColumn
                // 计算x
                let x = CGFloat(column) * (imageButton_W_H + imageMargin)
                // 计算y
                let y = CGFloat(row) * (imageButton_W_H + imageMargin)
                // 设置imageView的fame
                button.frame = CGRect(x: x, y: y, width: imageButton_W_H, height: imageButton_W_H)
                // 设置图片
//                button.sd_setImageWithURL(pictureArray![index], forState: UIControlState.Normal)
                button.sd_setBackgroundImageWithURL(pictureArray![index], forState: UIControlState.Normal)
            }
            index++
        }
        // 计算配图的大小
        if count == 0 { return CGSizeZero }
        // 总行数 = (count + 列数 - 1) / 列数  // 公式, 知道数据的总条数,一页显示多少条,算多少页
        let numberOfRow = (count + numberOfColumn - 1) / numberOfColumn
        // width = 列数 * imageView.width + (列数 - 1) * 间距
        let width = CGFloat(numberOfColumn) * imageButton_W_H + (CGFloat(numberOfColumn) - 1) * imageMargin
        // height = 行数 * imageView.height + (行数 - 1) * 间距
        let height = CGFloat(numberOfRow) * imageButton_W_H + (CGFloat(numberOfRow) - 1) * imageMargin
        
        return CGSize(width: width, height: height)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor=UIColor.cyanColor()
        setupUI()
    }
    // MARK: - 布局UI
    private func setupUI(){
        
        for _ in 0..<9{
            let imageButton = UIButton(type: UIButtonType.Custom)
            imageButton.backgroundColor=UIColor.randomColor()
            imageButton.contentMode=UIViewContentMode.ScaleAspectFill
            imageButton.clipsToBounds=true
            self.addSubview(imageButton)
            imageButtonArray.append(imageButton)
        }
    }
    // 存放9个imageView
    private var imageButtonArray = [UIButton]()
  
}
